﻿using System;
using System.ComponentModel;
using System.Drawing;
using ArtMath.Component.Interface;
using ArtMath.Attribute;
using ArtMath.Core.Data;
using ArtMath.Converter;
using ArtMath.PointCollection;
using ArtMath.Component.Point;
using ArtMath.Core.Helper;
using ArtMath.CommonType;

namespace ArtMath.Component.Polygon
{
    /// <summary>
    /// 正方形
    /// </summary>
    [Serializable]
    public class Square : ParallelogramBase, ICenter
    {
        PropertyDisplayer ctDisplayer;
        public Square(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.zfx, addToList)
        {
            CenterDisplayer = new PropertyDisplayer(this, "Center");
        }
        [MultilingualCategory("Parameter"), MultilingualDisplayName("Center"), TypeConverter(typeof(IntelliPointConverter))]
        public PointD Center
        {
            get { return BaryCenter; }
        }
        [Browsable(false)]
        public PointD PCenter
        {
            get { return PBaryCenter; }
        }
        [Browsable(false)]
        public PointD NewCenter
        {
            get { return NewBaryCenter; }
        }
        [Browsable(false)]
        public PointD NewPCenter
        {
            get { return NewPBaryCenter; }
        }

        [MultilingualCategory(null, "Sketchpad", "Display"), MultilingualDisplayName(null, "Center", "Displayer")]
        public PropertyDisplayer CenterDisplayer
        {
            get { return ctDisplayer; }
            set { ctDisplayer = value; }
        }
        protected override PointCollectionBase[] collections()
        {
            return new PointCollectionBase[] { new DiscontinuityPoint(Line1.Point1.ToPointD().RotateAt(Line2.Point1.ToPointD(), Core.Helper.Angle.RightAngle), Line1.Point1.ToPointD().RotateAt(Line2.Point1.ToPointD(), -Core.Helper.Angle.RightAngle)) };
        }
        public override PointCollectionBase[] GetInPointMovingRange(PointBase movePoint, Base sender = null)
        {
            switch (CountMovablePoints())
            {
                case 2:
                    if (movePoint == Line1.Point1)
                    {
                        if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.NewXY.RotateAt(Line4.Point1.NewXY, Core.Helper.Angle.RightAngle), Line3.Point1.NewXY.RotateAt(Line4.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line2.Point1.NewXY.RotateAt((Line2.Point1.NewXY + Line4.Point1.NewXY) / 2, Core.Helper.Angle.RightAngle), Line2.Point1.NewXY.RotateAt((Line2.Point1.NewXY + Line4.Point1.NewXY) / 2, -Core.Helper.Angle.RightAngle)) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.NewXY.RotateAt(Line2.Point1.NewXY, Core.Helper.Angle.RightAngle), Line3.Point1.NewXY.RotateAt(Line2.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                    }
                    else if (movePoint == Line2.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line4.Point1.NewXY.RotateAt(Line3.Point1.NewXY, Core.Helper.Angle.RightAngle), Line4.Point1.NewXY.RotateAt(Line3.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line4.Point1.NewXY.RotateAt(Line1.Point1.NewXY, Core.Helper.Angle.RightAngle), Line4.Point1.NewXY.RotateAt(Line1.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line1.Point1.NewXY.RotateAt((Line1.Point1.NewXY + Line3.Point1.NewXY) / 2, Core.Helper.Angle.RightAngle), Line1.Point1.NewXY.RotateAt((Line1.Point1.NewXY + Line3.Point1.NewXY) / 2, -Core.Helper.Angle.RightAngle)) };
                        }
                    }
                    else if (movePoint == Line3.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line4.Point1.NewXY.RotateAt((Line2.Point1.NewXY + Line4.Point1.NewXY) / 2, Core.Helper.Angle.RightAngle), Line4.Point1.NewXY.RotateAt((Line2.Point1.NewXY + Line4.Point1.NewXY) / 2, -Core.Helper.Angle.RightAngle)) };
                        }
                        else if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line1.Point1.NewXY.RotateAt(Line4.Point1.NewXY, Core.Helper.Angle.RightAngle), Line1.Point1.NewXY.RotateAt(Line4.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                        else if (Line4.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line1.Point1.NewXY.RotateAt(Line2.Point1.NewXY, Core.Helper.Angle.RightAngle), Line1.Point1.NewXY.RotateAt(Line2.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                    }
                    else if (movePoint == Line4.Point1)
                    {
                        if (Line1.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line2.Point1.NewXY.RotateAt(Line3.Point1.NewXY, Core.Helper.Angle.RightAngle), Line2.Point1.NewXY.RotateAt(Line3.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                        else if (Line2.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line3.Point1.NewXY.RotateAt((Line1.Point1.NewXY + Line3.Point1.NewXY) / 2, Core.Helper.Angle.RightAngle), Line3.Point1.NewXY.RotateAt((Line1.Point1.NewXY + Line3.Point1.NewXY) / 2, -Core.Helper.Angle.RightAngle)) };
                        }
                        else if (Line3.Point1.Movable)
                        {
                            return new PointCollectionBase[] { new DiscontinuityPoint(Line2.Point1.NewXY.RotateAt(Line1.Point1.NewXY, Core.Helper.Angle.RightAngle), Line2.Point1.NewXY.RotateAt(Line1.Point1.NewXY, -Core.Helper.Angle.RightAngle)) };
                        }
                    }
                    break;
            }
            return base.GetInPointMovingRange(movePoint, sender);
        }
        public override void RefreshPropertyDisplayersLocation()
        {
            CenterDisplayer.RefreshLocation();
            base.RefreshPropertyDisplayersLocation();
        }
        public override void RefreshPropertyDisplayersText()
        {
            CenterDisplayer.RefreshText();
            base.RefreshPropertyDisplayersText();
        }
        public override void PaintProperties(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible)
            {
                CenterDisplayer.Paint(gra);
            }
            base.PaintProperties(gra);
        }
        public override void ComponentBuilding(Base component)
        {
            if (component == Line2)
            {
                PointD tmp = Line1.Point1.ToPointD().RotateAt(Line2.Point1.ToPointD(), Core.Helper.Angle.RightAngle * Pad.Sign(Line1.Point1.Pxy, Line2.Point1.Pxy, Line2.SpareCoordinate));
                Line2.SpareCoordinate_ = Pad.XYToPxy(tmp);
            }
            base.ComponentBuilding(component);
        }
    }
}
